package com.longway.common.controller;

import com.jfinal.aop.Clear;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.longway.common.kit.CommonKit;
import com.longway.core.ext.render.picture.PictureRender;
import com.longway.core.toolbox.Func;

import java.io.File;
import java.util.List;

public class CommonController extends BaseController {

	@Clear
	public void renderImage() {
		String id = getPara(0);
		String file = Db.queryStr("select url from base_attach where id = ?", id);
		if (Func.isEmpty(file)) {
			redirect("/error/error404");
		} else {
			File f = new File(PropKit.get("baseUploadPath") + file);
			render(new PictureRender(f));
		}
	}
	
	/**
	 * 发送验证码
	 */
//	@Clear
//	public void sendCode(){
//		//手机号
//		String phone = getPara("phone");
//		if(StrKit.isBlank(phone)) {
//			renderJson(Ret.fail("msg", "手机号不能为空"));
//			return;
//		}
//		if(!RegexKit.isPhone(phone)){
//			renderJson(Ret.fail("msg", "手机号格式不正确"));
//			return;
//		}
//		//验证该手机是否已经被注册
//		phone = phone.toLowerCase().trim();
//		Integer cn = Db.queryInt("select id from base_user where phone = ? ", phone);
//		if (StrKit.notNull(cn)) {
//			renderJson(Ret.fail("msg", "手机号已被注册"));
//		}
//		String templateCode = getPara("templateCode");
//		int type = getInt("type");
//		int expireTime = getInt("expireTime");
//		Ret ret = SmsKit.sendCode(phone, templateCode, type, expireTime);
//		renderJson(ret);
//	}
	
	@Clear
	public void initLR(){
		List<String> ids = Db.query("select id||'' from td_device where pid = ?", 36574);
		for(String id : ids){
			System.out.println(id);
			CommonKit.levelSetLRChilds(1,0, id, "td_device","id","pid");
		}
		renderText("初始化左右值成功!");
	}
	
}
